
function text(x) {
    $.ajax({
        url: 'https://api.heweather.com/x3/weather',
        data: {
            city: x,
            key: "e77f5efcb30842e0a864f24c77ff4f62"
        },
        dataType: 'json',
        success: function (res) {
//                alert(res);
            verson = "HeWeather data service 3.0";
//                alert(res[verson][0].basic.lon);
            li = [];
            li = $(".next-top");
            for (i = 0; i < li.length+1; i++) {
                if(i===0){
                    $("#temp-top").text(res[verson][0].now.tmp + "℃）");
                    $("#today-condition").text(res[verson][0].now.cond.txt);
                    $("#spead").text("风速:" + res[verson][0].daily_forecast[i].wind.spd);
                    $("#today-win").text(res[verson][0].daily_forecast[i].wind.sc);
                    $("#top-day").text(res[verson][0].basic.city+res[verson][0].daily_forecast[i].date);
                    $("#today-temperature").text(res[verson][0].daily_forecast[0].tmp.min + "～" + res[verson][0].daily_forecast[i].tmp.max + "℃");
                    if(res[verson][0].now.cond.txt==="晴"){
                        $("#today-img").attr("src", "./img/1.png");
                    }
                    else if(res[verson][0].now.cond.txt==="阴"){
                        $("#today-img").attr("src", "./img/cloudy.png");
                    }
                    else if(res[verson][0].now.cond.txt==="多云"){
                        $("#today-img").attr("src", "./img/Lcloudy.png");
                    }
                    else if(res[verson][0].now.cond.txt==="小雨"){
                        $("#today-img").attr("src", "./img/Lrain.png");
                    }
                    else if(res[verson][0].now.cond.txt==="中雨"){
                        $("#today-img").attr("src", "./img/Mrain.png");
                    }
                    else if(res[verson][0].now.cond.txt==="大雨"){
                        $("#today-img").attr("src", "./img/Brain.png");
                    }
                    else if(res[verson][0].now.cond.txt==="小雪"){
                        $("#today-img").attr("src", "./img/Lsnow.png");
                    }
                    else if(res[verson][0].now.cond.txt==="中雪"){
                            $("#today-img").attr("src", "./img/Msnow.png");
                    }
                    else if(res[verson][0].now.cond.txt==="大雪"){
                        $("#today-img").attr("src", "./img/Brain.png");
                    }

                }

                    $("#next-top-" + (i+1)).text(res[verson][0].daily_forecast[i+1].date);
                    $("#next-cond-" + (i+1)).text(res[verson][0].daily_forecast[i+1].cond.txt_d);
                    $("#next-wind-" + (i+1)).text(res[verson][0].daily_forecast[i+1].wind.sc);
                    $("#next-tem-"+(i+1)).text(res[verson][0].daily_forecast[i+1].tmp.min+"～"+res[verson][0].daily_forecast[i+1].tmp.max+"℃");
                if(res[verson][0].daily_forecast[i+1].cond.txt_d==="晴"){
                    $("#next-img-"+(i+1)).attr("src", "./img/1.png");
                }
                else if(res[verson][0].daily_forecast[i+1].cond.txt_d==="多云"){
                    $("#next-img-"+(i+1)).attr("src", "./img/Lcloudy.png");
                }
                else if(res[verson][0].daily_forecast[i+1].cond.txt_d==="阴"){
                    $("#next-img-"+(i+1)).attr("src", "./img/cloudy.png");
                }
                else if(res[verson][0].daily_forecast[i+1].cond.txt_d==="小雨"){
                    $("#next-img-"+(i+1)).attr("src", "./img/Lrain.png");
                }
                else if(res[verson][0].daily_forecast[i+1].cond.txt_d==="中雨"){
                    $("#next-img-"+(i+1)).attr("src", "./img/Mrain.png");
                }
                else if(res[verson][0].daily_forecast[i+1].cond.txt_d==="大雨"){
                    $("#next-img-"+(i+1)).attr("src", "./img/Brain.png");
                }
                else if(res[verson][0].daily_forecast[i+1].cond.txt_d==="小雪"){
                    $("#next-img-"+(i+1)).attr("src", "./img/Lsnow.png");
                }
                else if(res[verson][0].daily_forecast[i+1].cond.txt_d==="中雪"){
                    $("#next-img-"+(i+1)).attr("src", "./img/Msnow.png");
                }
                else if(res[verson][0].daily_forecast[i+1].cond.txt_d==="大雪"){
                    $("#next-img-"+(i+1)).attr("src", "./img/Bsnow.png");
                }

            }
        }
    })
}

//var x=$("#top-day").text();
//text("绵阳");
//text($("#top-day").text());

/*城市选择器*/
var cityChange= eval(cityChange);
$(function(){
    $('#index_province').change(function(){
        for(var i in cityChange){
            if(i==this.value){
                var index_city_obj = $('#index_city')[0];
                index_city_obj.innerHTML='';
                var obj = cityChange[i];
                for(var k in obj){
                    if(obj[k].name){
                        index_city_obj.options[index_city_obj.options.length] = new Option( obj[k].name,obj[k].pinyin);
                    }
                }
                break;
            }
        }
    });
})
$(function() {
    $('#city_name').autocomplete(cities, {
        max: 12, //列表里的条目数
        minChars: 0, //自动完成激活之前填入的最小字符
        width: 385, //提示的宽度，溢出隐藏
        scrollHeight: 300, //提示的高度，溢出显示滚动条
        matchContains: true, //包含匹配，就是data参数里的数据，是否只要包含文本框里的数据就显示
        autoFill: false, //自动填充
        minChars:1,
        formatItem: function(row, i, max) {
            return row.name + '（'+row.pinyin+'）';
        },
        formatMatch: function(row, i, max) {
            return row.match;
        },

        formatResult: function(row) {
            return row.name;
        },resultsClass:'search-text'
    }).result(function(event, row, formatted) {
        $("#hid_city_name").val(row.pinyin);
        $("#hid_real_city_name").val(row.name);
        $('#pop_cities').hide();
    });
});
function append_city(){
    $('#cityarea').show();
    if($("#citylist").text()==""){
        $("#citylist").append(cityarea);
    }
}
$(document).ready(function(){
    $(document).bind('click', hide_div);
});

function hide_div(e){
    var biaoqian = "click";
    var classname= $(e.target)[0].className;
    if(classname.indexOf('click')>'-1' ||$(e.target)[0].id=='city_name')
        return ;

    if($('#city_name').val()=='')
    {
        $('#city_name').val('请输入城市中文或拼音 / 点击选择城市');
        $('#city_name').css('color','#B7B7B7');
    }
    $("#cityarea").hide();
}
//直接输入地址框的onblur事件
function input_blur()
{
    var value = $('#city_name').val();
    var all_city_val = $(".ac_over").text();
    if(all_city_val && all_city_val!="")
    {
        var str = all_city_val.substr(0,(all_city_val.length-1));
        strs=str.split("（");
        $("#hid_city_name").val(strs[1]);
        $("#hid_real_city_name").val(strs[0]);
        $("#city_name").val(strs[0]);
        $(".search-text").hide();
    }else if($.trim(value)==''&& $('#cityarea').css('display')=='none')
    {
        $('#city_name').val('请输入城市中文或拼音 / 点击选择城市');
        $('#city_name').css("color","#B7B7B7");
    }
}

//直接输入地址框的onkeyup事件
function input_keyup()
{
    var value = $('#city_name').val();
    if($('#hid_real_city_name').val()!=value || $('#hid_real_city_name').val()=='')
    {
        $('#cityarea').hide();
    }else if(value==$('#hid_real_city_name').val())
    {
        $('#cityarea').hide();
    }
}
function check_input(){
    var value = $('#city_name').val();
    if(value==$('#hid_real_city_name').val() && $("#hid_city_name").val()!=""){
        return true;
    }
    return false;
}
/**
 * 字母页面内链
 *
 */
function letter_scroll(letter)
{
    var obj = $("#c_"+letter);
    $('html,body').animate({scrollTop: obj.offset().top}, 500);
}
/*
 * 用户点击城市后，城市名填入input框
 */
function change_city_val(city, pinyin_city)
{
    $('#city_name').css({ color: "#606060"});
    $('#city_name').val(city);
    $('#hid_city_name').val(pinyin_city);
    $("#hid_real_city_name").val(city);
    $('#cityarea').hide();
    text(city);
}
function tabCutover(c,d){$(c).parent().attr("class");$(c).parent().children().removeClass("current");$(c).addClass("current");$("."+d).parent().children().hide();$("."+d).show();}


var map = new AMap.Map("mapContainer", {
    resizeEnable: true,
    //二维地图显示视口
    //地图中心点
    center: [116.397428, 39.90923],
    //地图显示的缩放级别
    zoom: 13
});
//获取用户所在城市信息
function showCityInfo() {
    //加载城市查询插件
    AMap.service(["AMap.CitySearch"], function() {
        //实例化城市查询类
        var citysearch = new AMap.CitySearch();
        //自动获取用户IP，返回当前城市
        citysearch.getLocalCity(function(status, result) {
            if (status === 'complete' && result.info === 'OK') {
                if (result && result.city && result.bounds) {
                    var cityinfo = result.city;
                    var citybounds = result.bounds;
                    //s=s.ToString(cityinfo).RTrim('市');
                    //var str=$("#info").text();
                    //var newstr=str.substring(0,str.length-1);
                    var str=cityinfo;
                    cityinfo=str.substring(0,str.length-1);
                    text(cityinfo);

                    document.getElementById('top-day').innerHTML =  cityinfo;
                    //地图显示当前城市
                    //map.setBounds(citybounds);
                }
            }
        });
    });

}
showCityInfo();

//text($("#top-day").text());
